<?php
namespace jy_merchteamfee;
defined('BY_JYA') or exit('error');
/*
	团队长分红计算
	
*/
class commission extends \plugin{
	/*
		生成分红
	*/
	function create($order){
		if(!$this->setting['isopen']){
			return;
		}
		$this->order=$order;
		$_SESSION['onemerchid']=$order['merchid'];
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'order_price'=>$this->order['price'],
			'commission_money'=>0,
			'red_money'=>0,
			'daili_money'=>0,
			'ping_money'=>0,
			'createtime'=>TIMESTAMP,
		);
		//1、默认方式: 除运费和会员折扣(或促销折扣)外其他所有费用全部计算分红；实际支付方式: 只计算实际支付和余额抵扣部分的分红(包括余额支付)
		if($this->setting['commissiontype']==2){
			$this->commission_order['commission_price']=$this->order['price'];
		}else{
			$this->commission_order['commission_price']=$this->order['price']-$this->order['dispatchprice'];
		}
		$myteam=$this->get('team',array('mid'=>$this->order['mid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
		if($myteam['id']){
			$fromuser=$myteam['fromuser'];
		}else{
			$fromuser=$this->getcolumn('member',array('mid'=>$this->order['mid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),'fromuser');
		}
		$this->commission_order['teamid']=$fromuser;
		//直推佣金
		$team=$this->get('team',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
		$team['fromuser']=$this->getcolumn('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),'fromuser');
		$this->create_commission($team);		
		if($myteam['level_id']){
			$this->nowlevel=$this->getcolumn('level',array('id'=>$myteam['level_id']),'level');
		}else{
			$this->nowlevel=0;
		}
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);		
		$levels=$this->get_list('level',$parameter);
		$minlevel=$levels[0]['level'];
		$parameter['orderby']='level desc';
		$levels=$this->get_list('level',$parameter);
		$this->commission_order['maxlevel']=$maxlevel=$levels[0]['level'];
		$levelnum=12;	
		//极差分红单线等级ABCDE，第一级没有分红，低于自己的没有分红，平级没有分红
		for($i=0;$i<$levelnum;$i++){
				if(!$fromuser){
					break;
				}
				$team=$this->get('team',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
				$fromuser=$this->getcolumn('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),'fromuser');
				$level=$this->getcolumn('level',array('id'=>$team['level_id']),'level');
				$level=$level?:0;
				if($this->setting['teamred_open'] && !$this->isteamred && $team['is_teamred']){
					$this->create_teamred($team);
				}
				//上等级不是最大级，当前等级小于上等级或小于最小等级，都没有分红
				if($this->nowlevel!=$maxlevel && ($level<=$minlevel || $level<$this->nowlevel)){
					continue;
				}
				if($this->ismaxping && $level==$maxlevel){
					continue;
				}
				if($level==$this->nowlevel){
					$isping=1;
				}
				$this->create_redcommission($team,$level,$isping);
				//上次是最大级，并且当前等级也是最大
				if($this->nowlevel==$maxlevel && $level==$maxlevel){
					$this->ismaxping=1;
				}
				//当等级匹配时，分红
				$this->nowlevel=$level;
				//如果他的上级是我
				$ta_agent_id=$this->getcolumn('member',array('mid'=>$fromuser,'merchid'=>$_SESSION['onemerchid']),'fromuser');
				if($ta_agent_id==$team['mid']){
					break;
				}
		}
		//生成分销订单
		$indata=$this->commission_order;
		unset($indata['isarea']);
		unset($indata['maxlevel']);
		unset($indata['red_money']);
		$this->insert('order',$indata);
		//自购升级
		if($myteam['id']){
			$this->model('member')->update_agent_level($myteam['mid'],true);
		}
		return true;
	}
	/*
		直推分佣type=0;
	*/
	function create_commission($agent,$key_level=''){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				//获取默认等级分红比例
				if($_SESSION['onemerchid']){
					$level=$this->plugin_setting('default_level'.$_SESSION['onemerchid']);
				}else{
					$level=$this->plugin_setting('default_level');
				}
				$level_id=0;
			}else{
				$level=$this->get('level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_id=$level['id'];
			}
			//有上级
			if($level['fee_per2']>0 && $agent['fromuser']==0){
				$commission_per=$level['fee_per2'];
				$isnotwo=1;
			}else{
				$commission_per=$level['fee_per'];
			}
			$demojindoufee=$level['jindoufee'];
			$money=0;
			$jifen=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);			
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$commission_level='commission'.$level_id;
			$jindoufeelevel='jindoufee'.$level_id;
			foreach($this->order['goods'] as $goods){
				$ignore=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
				if($ignore['ignore']  || !$ignore['id']){
					$goods_commission=array();
					if($ignore['ignore']){
						$order_price=$order_price-$total_price;
					}
					$this->needorder=true;
					continue;
				}else{
					$goods_commission=$ignore['commissions']?json_decode($ignore['commissions'],true):array();
				}
				$order_price=$order_price-$goods['total_price'];
				//有上级
				if($goods_commission[$commission_level.'2']>0 && $agent['fromuser']==0){
					$commission_level=$commission_level.'2';
					$isnotwo=1;
				}
				$goods_commission_per=$goods_commission[$commission_level];
				if(substr($goods_commission_per,-1)=='%'){
					$goods_commission_money=intval($goods['total_price']*substr($goods_commission_per,0,-1))/100;
					$money+=$goods_commission_money;
				}else{
					$goods_commission_money=intval($goods_commission_per*$goods['total']*100)/100;
					$money+=$goods_commission_money;
				}
				//积分
				$jindoufee=$goods_commission[$jindoufeelevel];
				if(substr($jindoufee,-1)=='%'){
					$goods_jifen=intval($goods['total_price']*substr($jindoufee,0,-1))/100;
					$jifen+=$goods_jifen;
				}else{
					$goods_jifen=intval($jindoufee*$goods['total']*100)/100;
					$jifen+=$goods_jifen;
				}
				$remark['goods_commission'][]=array('goods_id'=>$goods['goodsid'],'total_price'=>$goods['total_price'],'total'=>$goods['total'],'commission_per'=>$goods_commission_per,'commission_money'=>$goods_commission_money);
			}
			if($order_price>0 && $this->needorder){
				$order_commission_money=intval($order_price*$commission_per)/100;
				$money+=$order_commission_money;
				//积分
				$demo_jifen=intval($order_price*$demojindoufee)/100;
				$jifen+=$demo_jifen;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
			}
			$remark['commission_money']=$money;
			if($money>0 || $jifen>0){
				$this->commission_order['commission_money']+=$money;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$_SESSION['onemerchid'],
					'mid'=>$agent['mid'],
					'agent_id'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>$level_id,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'jifen'=>$jifen,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
					'type'=>$isnotwo?3:0,
				);
				$this->insert('commission',$new_commission);
			}
			if($jifen>0){
				model('core/member')->member_credit($agent['mid'],'credit3',$jifen,true,'jy_merchteamfee','团队金豆奖励');
			}
			//判断是否存在团队长
			$this->commission_order['isarea']=true;
			//此处判断分销商是否满足升级条件，如果满足则升级分销商等级
			$agent_update=array(
				'order_price +='=>$this->commission_order['order_price'],
				'order_num +='=>1,
				'commission_money +='=>$money,
			);
			$this->update('team',$agent_update,array('id'=>$agent['id']));
			$this->model('member')->update_agent_level($agent['mid']);
	}
	/*
		团队总分红;
	*/
	function create_teamred($agent){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				//获取默认等级分红比例
				if($_SESSION['onemerchid']){
					$level=$this->plugin_setting('default_level'.$_SESSION['onemerchid']);
				}else{
					$level=$this->plugin_setting('default_level');
				}
				$level_id=0;
			}else{
				$level=$this->get('level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_id=$level['id'];
			}
			$commission_per=$level['teamredfee'];
			$money=0;
			$jifen=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);			
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$commission_level='teamredfee'.$level_id;
			foreach($this->order['goods'] as $goods){
				$ignore=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
				if($ignore['ignore']  || !$ignore['id']){
					$goods_commission=array();
					if($ignore['ignore']){
						$order_price=$order_price-$total_price;
					}
					$this->needorder=true;
					continue;
				}else{
					$goods_commission=$ignore['commissions']?json_decode($ignore['commissions'],true):array();
				}
				$order_price=$order_price-$goods['total_price'];
				$goods_commission_per=$goods_commission[$commission_level];
				if(substr($goods_commission_per,-1)=='%'){
					$goods_commission_money=intval($goods['total_price']*substr($goods_commission_per,0,-1))/100;
					$money+=$goods_commission_money;
				}else{
					$goods_commission_money=intval($goods_commission_per*$goods['total']*100)/100;
					$money+=$goods_commission_money;
				}
				$remark['goods_commission'][]=array('goods_id'=>$goods['goodsid'],'total_price'=>$goods['total_price'],'total'=>$goods['total'],'commission_per'=>$goods_commission_per,'commission_money'=>$goods_commission_money);
			}
			if($order_price>0 && $this->needorder){
				$order_commission_money=intval($order_price*$commission_per)/100;
				$money+=$order_commission_money;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
			}
			$remark['commission_money']=$money;
			if($money>0){
				$this->commission_order['commission_money']+=$money;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$_SESSION['onemerchid'],
					'mid'=>$agent['mid'],
					'agent_id'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>$level_id,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
					'type'=>7,
				);
				$this->insert('commission',$new_commission);
				$this->isteamred=1;
			}
			//判断是否存在团队长
			$this->commission_order['isarea']=true;
			return true;
	}
	/*
		级差分佣type=1;
	*/
	function create_redcommission($agent,$key_level='',$isping=0){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				//获取默认等级分红比例
				if($_SESSION['onemerchid']){
					$level=$this->plugin_setting('default_level'.$_SESSION['onemerchid']);
				}else{
					$level=$this->plugin_setting('default_level');
				}
				$level_id=0;
			}else{
				$level=$this->get('level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level['setdata']=json_decode($level['setdata'],true);
				$level_id=$level['id'];
				
			}
			//开启最大平级分红？
			if($this->setting['open_maxteamfee'] && $key_level==$this->commission_order['maxlevel'] && $this->nowlevel==$key_level){
				$commission_per=$this->setting['maxteamfee_rate'];
				$ismaxred=true;
			}else{
				$commission_per=$level['red_per'];
			}
			$money=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);			
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$commission_level='red'.$level_id;
			$daili_level='daili'.$level_id;
			$daili_titype='daili_titype'.$level_id;
			$daili_zhuanjifen='daili_zhuanjifen'.$level_id;
			$pingfee_level='pingfee'.$level_id;
			$pingfee_titype='pingfee_titype'.$level_id;
			$pingfee_zhuanjifen='pingfee_zhuanjifen'.$level_id;
			$this->needorder=false;
			foreach($this->order['goods'] as $goods){
				$ignore=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
				if(!$ignore['id']){
					$this->needorder=true;
					continue;
				}
				if($ignore['ignore']){
					$goods_commission=array();
					$order_price=$order_price-$goods['total_price'];
					continue;
				}else{
					$goods_commission=$ignore['redfee']?json_decode($ignore['redfee'],true):array();
				}
				$order_price=$order_price-$goods['total_price'];
				//开启最大平级分红？
				if($ignore['open_maxteamfee'] && $ismaxred){
					$goods_commission_per=$ignore['maxteamfee_rate'];
				}else{
					$goods_commission_per=$goods_commission[$commission_level];
				}
				if(substr($goods_commission_per,-1)=='%'){
					$goods_commission_money=intval($goods['total_price']*substr($goods_commission_per,0,-1))/100;
					if(!$ismaxred){
						$goods_commission_money -=$this->commission_order['red_money'];
					}
					$money+=$goods_commission_money;
				}else{
					$goods_commission_money=intval($goods_commission_per*$goods['total']*100)/100;
					if(!$ismaxred){
						$goods_commission_money -=$this->commission_order['red_money'];
					}
					$money+=$goods_commission_money;
				}
				$goods_daili_per=$goods_commission[$daili_level];
				$goods_pingfee_per=$goods_commission[$pingfee_level];
				//代理奖金
				if(substr($goods_daili_per,-1)=='%'){
					$goods_daili_money=intval($goods['total_price']*substr($goods_daili_per,0,-1))/100;
					$goods_daili_money -=$this->commission_order['daili_money'];
				}else{
					$goods_daili_money=intval($goods_daili_per*$goods['total']*100)/100;
					$goods_daili_money -=$this->commission_order['daili_money'];				
				}
				if(!$goods_commission[$daili_titype] && $goods_commission[$daili_zhuanjifen]){
					$goods_daili_jifen=intval($goods_daili_money*$goods_commission[$daili_zhuanjifen])/100;
					$daili_jifen +=$goods_daili_jifen;
				}
				$goods_daili_jifen=$goods_daili_jifen?:0;
				$goods_daili_money=$goods_daili_money-$goods_daili_jifen;
				$daili_money +=$goods_daili_money;
				//平级奖金
				if($isping){
					if(substr($goods_pingfee_per,-1)=='%'){
						$goods_pingfee_money=intval($goods['total_price']*substr($goods_pingfee_per,0,-1))/100;
						$goods_pingfee_money -=$this->commission_order['ping_money'];
					}else{
						$goods_pingfee_money=intval($goods_pingfee_per*$goods['total']*100)/100;
						$goods_pingfee_money -=$this->commission_order['ping_money'];
					}
					if(!$goods_commission[$pingfee_titype] && $goods_commission[$pingfee_zhuanjifen]){
						$goods_pingfee_jifen=intval($goods_pingfee_money*$goods_commission[$pingfee_zhuanjifen])/100;
						$ping_jifen +=$goods_pingfee_jifen;
					}
					$goods_pingfee_jifen=$goods_pingfee_jifen?:0;
					$goods_pingfee_money=$goods_pingfee_money-$goods_pingfee_jifen;
					$ping_money +=$goods_pingfee_money;
				}
				$remark['goods_commission'][]=array('goods_id'=>$goods['goodsid'],'total_price'=>$goods['total_price'],'total'=>$goods['total'],'commission_per'=>$goods_commission_per,'commission_money'=>$goods_commission_money);
			}
			if($order_price>0 && ($this->needorder|| $ismaxred)){
				$order_commission_money=intval($order_price*$commission_per)/100;
				if(!$ismaxred){
					$order_commission_money -=$this->commission_order['red_money'];
				}
				$money+=$order_commission_money;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
				$goods_daili_per=$level['setdata']['daili_fee'];
				//代理奖金
				if(substr($goods_daili_per,-1)=='%'){
					$goods_daili_money=intval($order_price*substr($goods_daili_per,0,-1))/100;
					$goods_daili_money -=$this->commission_order['daili_money'];
				}else{
					$goods_daili_money=intval($goods_daili_per*$order_price*100)/100;
					$goods_daili_money -=$this->commission_order['daili_money'];
				}
				if(!$level['setdata']['daili_titype'] && $level['setdata']['daili_zhuanjifen']){
					$goods_daili_jifen=intval($goods_daili_money*$level['setdata']['daili_zhuanjifen'])/100;
					$daili_jifen +=$goods_daili_jifen;
				}
				$goods_daili_jifen=$goods_daili_jifen?:0;
				$goods_daili_money=$goods_daili_money-$goods_daili_jifen;
				$daili_money +=$goods_daili_money;
				$goods_pingfee_per=$level['setdata']['pingfee_rate'];
				//平级奖金
				if($isping){
					if(substr($goods_pingfee_per,-1)=='%'){
						$goods_pingfee_money=intval($goods['total_price']*substr($goods_pingfee_per,0,-1))/100;
						$goods_pingfee_money -=$this->commission_order['ping_money'];
						$ping_money +=$goods_pingfee_money;
					}else{
						$goods_pingfee_money=intval($goods_pingfee_per*$goods['total']*100)/100;
						$goods_pingfee_money -=$this->commission_order['ping_money'];
						$ping_money +=$goods_pingfee_money;
					}
					if(!$level['setdata']['pingfee_titype'] && $level['setdata']['pingfee_zhuanjifen']){
						$goods_pingfee_jifen=intval($goods_pingfee_money*$level['setdata']['pingfee_zhuanjifen'])/100;
						$ping_jifen +=$goods_pingfee_jifen;
					}
					$goods_pingfee_jifen=$goods_pingfee_jifen?:0;
					$goods_pingfee_money=$goods_pingfee_money-$goods_pingfee_jifen;
					$daili_money +=$goods_pingfee_money;
				}
			}
			$remark['commission_money']=$money;
			if($money>0){
				$this->commission_order['commission_money']+=$money;
				if(!$ismaxred){
					$this->commission_order['red_money']+=$money;
				}
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$_SESSION['onemerchid'],
					'mid'=>$agent['mid'],
					'agent_id'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>$level_id,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
					'type'=>$ismaxred?2:1,
				);
				$this->insert('commission',$new_commission);
			}
			$check_canaddfee=$this->model('member')->check_canaddfee($agent['mid'],5);
			if(($daili_money>0 || $daili_jifen>0) && $check_canaddfee){
				$this->commission_order['daili_money']+=$daili_money;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'agent_id'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>$level_id,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$daili_money,
					'jifen'=>$daili_jifen,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
					'type'=>5,
					'merchid'=>$_SESSION['onemerchid']
				);
				$this->insert('commission',$new_commission);
			}
			$check_canaddfee=$this->model('member')->check_canaddfee($agent['mid'],6);
			if(($ping_money>0 || $ping_jifen>0) && $check_canaddfee){
				$this->commission_order['ping_money']+=$ping_money;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'agent_id'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>$level_id,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$ping_money,
					'jifen'=>$ping_jifen,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
					'type'=>6,
					'merchid'=>$_SESSION['onemerchid']
				);
				$this->insert('commission',$new_commission);
			}
			//判断是否存在团队长
			$this->commission_order['isarea']=true;
			//此处判断分销商是否满足升级条件，如果满足则升级分销商等级
			$agent_update=array(
				'order_price +='=>$this->commission_order['order_price'],
				'order_num +='=>1,
				'commission_money +='=>$money,
			);
			$this->update('team',$agent_update,array('id'=>$agent['id']));
			$this->model('member')->update_agent_level($agent['mid']);
	}
	/*
		订单完成时，把分红状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('status'=>0,'ordersn'=>$order['ordersn']));
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		$type=intval($post['type']);
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
		}
		//获取可结算分红
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		if($this->setting['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		if($type){
			$params['type']=$type;
			$isgetfee=$this->model('member')->check_isgetfee($mid,$type);
			if($isgetfee){
				exi('您已结算过了该奖金！不能再结算！','error');
			}
		}else{
			$params['type <=']=3;
		}
		$commissions=$this->getall('commission',$params);
		if(!$commissions){
			exi('没有可结算的分红！','error');
		}
		$commission_num=count($commissions);
		$money=0;
		$jifen=0;
		foreach($commissions as $commission){
			$money+=$commission['money'];
			$jifen+=$commission['jifen'];
		}
		//结算最低分红检测
		if($money<$this->setting['balance_fee']){
			exi('满￥'.$this->setting['balance_fee'].'才可结算！','error');
		}
		if($type==5){
			if($this->setting['jie_credit']!=1){
				$post['pay_type']='credit';
			}else{
				$post['pay_type']='jifen';
			}
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'jifen'=>$jifen,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'createtime'=>TIMESTAMP,
			'fee_type'=>$type,
			'status'=>0,
			'merchid'=>$_SESSION['onemerchid']
		);
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		foreach($commissions as $commission){
			$update=array(
				'status'=>2,
				'balancesn'=>$new_balance['balancesn'],
			);
			$this->update('commission',$update,array('id'=>$commission['id']));
		}
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn']);
		$update=array(
			'status'=>3,
			'pay_type'=>$pay_type,
			'pay_trade_no'=>$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		$this->update('balance',$update,array('id'=>$balance['id']));
		if($balance['jifen']>0){
			cfc('core')->member_credit($balance['mid'],'credit1',$balance['jifen'],true,$this->plugin_name,$remark='佣金转积分');
		}
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销分红打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn){
		$params=array(
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'remark'=>'代理商分红发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no']?:$res['batch_id'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return '';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return '';
		exi('暂不支持打款到银行账户！','error');
	}

	public function commission_remark($remark){
		if(!$remark){
			return '';
		}
		$remark=json_decode($remark,true);
		$goods_str='';
		if($remark['goods_commission']){
			foreach($remark['goods_commission'] as $goods){
				$goods['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goods_id']),'title');
				$goods_str.="商品ID【{$goods['goods_id']}】，商品名称【{$goods['title']}】，商品数量【{$goods['total']}】，商品总价【{$goods['total_price']}】，分红规则【{$goods['commission_per']}】，分销分红【{$goods['commission_money']}】\n";
			}
		}else{
			$goods_str='';
		}
		$remark['level_title']=pdo_getcolumn('jy_merchteamfee_level',array('uniacid'=>$_SESSION['uniacid'],'level'=>$remark['level_id']),'title');
		$remark['level_title']=$remark['level_title']?:'默认等级';
		$remark_str=<<<TPL
团长等级ID【{$remark['level_id']}】，等级名称【{$remark['level_title']}】，分红等级【{$remark['commission_level']}】，订单总价【{$remark['order_price']}】
商品独立分红：
{$goods_str}
订单分红：
除去设置独立分红的商品外订单金额【{$remark['order_commission']['order_price']}】，分红规则【{$remark['order_commission']['commission_per']}%】，分销分红【{$remark['order_commission']['commission_money']}】
分红总计：【{$remark['commission_money']}】
TPL;
		return $remark_str;
	}
}